package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workcell.workdata.xthings.dto.PageVo;
import com.workcell.workdata.xthings.dto.TenantDto;
import com.workcell.workdata.xthings.dto.TenantVo;
import com.workcell.workdata.xthings.entity.Tenant;
import com.workcell.workdata.xthings.service.TenantService;
import com.workcell.workdata.xthings.mapper.TenantMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Admin
* @description 针对表【t_tenant(租户表)】的数据库操作Service实现
* @createDate 2023-05-15 14:58:17
*/
@Service
@RequiredArgsConstructor
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant>
    implements TenantService{
    private final TenantMapper tenantMapper;


    @Override
    public List<TenantDto> tenantList(TenantVo tenantVo) {
        return tenantMapper.tenantList(tenantVo);
    }

    @Override
    public PageInfo<TenantDto> tenantPage(PageVo pageVo, TenantVo tenantVo) {
        Page<Object> page = PageHelper.startPage(pageVo);
        List<TenantDto> list = this.tenantList(tenantVo);
        PageInfo<TenantDto> pageInfo = new PageInfo<>(list);
        page.close();
        return pageInfo;
    }

    @Override
    public Tenant selectByTenantCode(String tenantCode) {
        LambdaQueryWrapper<Tenant> lambdaQueryWrapper = Wrappers.lambdaQuery(Tenant.class)
                .eq(Tenant::getTenantCode, tenantCode);
        return tenantMapper.selectOne(lambdaQueryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(TenantVo tenantVo) {
        Tenant oldTenant = this.selectByTenantCode(tenantVo.getTenantCode());
        Assert.isNull(oldTenant,"租户id：{}已存在", tenantVo.getTenantCode());
        // 如果租户简称为空，将租户全称设置为租户简称
        tenantVo.setTenantShortName(StrUtil.emptyToDefault(tenantVo.getTenantShortName(), tenantVo.getTenantFullName()));
        Tenant tenant = Tenant.builder()
                .tenantCode(tenantVo.getTenantCode())
                .tenantShortName(tenantVo.getTenantShortName())
                .tenantFullName(tenantVo.getTenantFullName())
                .build();
        return this.save(tenant);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delete(String tenantCode) {
        Tenant tenant = this.selectByTenantCode(tenantCode);
        Assert.notNull(tenant, "租户id：{}不存在", tenantCode);

        LambdaUpdateWrapper<Tenant> lambdaUpdateWrapper = Wrappers.lambdaUpdate(Tenant.class)
                .eq(Tenant::getTenantCode, tenantCode);
        return tenantMapper.delete(lambdaUpdateWrapper) > 0;
    }

    @Override
    public List<Tenant> selectList(TenantVo tenantVo) {
        String tenantCode = tenantVo.getTenantCode();
        String tenantShortName = tenantVo.getTenantShortName();
        String tenantFullName = tenantVo.getTenantFullName();
        LambdaQueryWrapper<Tenant> lambdaQueryWrapper = Wrappers.lambdaQuery(Tenant.class)
                .like(StrUtil.isNotEmpty(tenantCode), Tenant::getTenantCode, tenantCode)
                .like(StrUtil.isNotEmpty(tenantShortName), Tenant::getTenantShortName, tenantShortName)
                .like(StrUtil.isNotEmpty(tenantFullName), Tenant::getTenantFullName, tenantFullName)
                .orderByAsc(Tenant::getId);
        return tenantMapper.selectList(lambdaQueryWrapper);
    }
}




